// const sqlite3 = require('sqlite3').verbose()
const path = require('path')
const fs = require('fs')
const compressing = require('compressing')
var { remote } = require('electron')
var conn=()=>{}
// let db
//
// // 连接数据库
// function conn () {
//   if (!db || !db.open) {
//     db = new sqlite3.Database('base.db')
//   }
//   return db
// }

// uuid
function generateUUID () {
  let d = new Date().getTime()
  if (window.performance && typeof window.performance.now === 'function') {
    // use high-precision timer if available
    d += performance.now()
  }
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = (d + Math.random() * 16) % 16 | 0
    d = Math.floor(d / 16)
    return (c === 'x' ? r : (r & 0x3 | 0x8)).toString(16)
  })
  return uuid
}

// 初始化数据表
export const initTable = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.serialize(() => {
      // 建表 -- 密码 PassWord 操作时间 uploadTime
      db.run('CREATE TABLE if not exists PassWord (id int primary key, password varchar(64), uploadTime varchar(64))')
      // 建表 -- 反馈意见 Feedback(问题分类 ProblemClassification,问题方面 Problems,具体问题 concreteProblems, 备注 remake)
      db.run('CREATE TABLE if not exists Feedback (id int primary key, ' +
        'ProblemClassification varchar(64), Problems varchar(64), concreteProblems varchar(64), remake varchar(64))')
      // 建表 -- 反馈公开 PublicityOfFeedback(标题 title,编辑人 editedBy,内容 content,附件 enclosure,操作时间 uploadTime)
      db.run('CREATE TABLE IF NOT EXISTS PublicityOfFeedback (' +
        'id int primary key, ' +
        'title varchar(64), ' +
        'editedBy varchar(64), ' +
        'content varchar(64), ' +
        'enclosure varchar(64), ' +
        'uploadTime varchar(64))')
      /**
       * 建表 -- 整改会议 => 研究整改工作 RectificationMeeting(
       * 会议主题 theme,会议类型 meetingType,
       * 会议日期 dateOfMeeting,会议时间 startMeetingTime ednMeetingTime,
       * 主持人 host,参会人员 participants,
       * 审批状态 spstatus,处理状态（暂存【处理中】，保存【已处理】） clstatus,
       * 审批记录 approvalRecord,
       * 会议提纲 meetingSummary,会议材料 enclosure,操作时间 uploadTime
       * )
      */
      db.run('CREATE TABLE IF NOT EXISTS RectificationMeeting ' +
        '(id int primary key, theme varchar(64), meetingType varchar(64),  ' +
        'dateOfMeeting varchar(64), startMeetingTime varchar(64), ednMeetingTime varchar(64), ' +
        'host varchar(64), participants varchar(64), ' +
        'spstatus varchar(64), clstatus varchar(64), ' +
        'approvalRecord varchar(2048),' +
        'meetingSummary varchar(64), enclosure varchar(64), uploadTime varchar(64))')
      /**
       * 建表 -- 整改方案详情 RectificationPlan(
       * 标题 title,
       * 编辑人 editor,联系电话 telephone,
       * 审批状态 spstatus,处理状态（暂存【处理中】，保存【已处理】） clstatus,
       * 整改方案 rectificationPlan,派驻组评估情况 pzzAssess,
       * 监督检查室评估情况 jcsAssess,操作时间 uploadTime,)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationPlan (id int primary key, editor varchar(64), ' +
        'title varchar(64),' +
        'telephone varchar(64), spstatus varchar(64), clstatus varchar(64), ' +
        'rectificationPlan varchar(64), pzzAssess varchar(64), ' +
        'jcsAssess varchar(64), uploadTime varchar(64))')
      /**
       * 建表 -- 整改方案列表 RectificationPlanList(
       * 问题分类 ProblemClassification,问题方面 Problems,
       * 反馈问题 FeedbackIssues,整改目标及具体措施 target,
       * 责任领导 leadership,责任人 personLiable,
       * 完成时限 timeLimitFoRCompletion, 派驻组评估情况 pzzAssess,
       * 具体问题 concreteProblems, 监督检查室评估情况 jcsAssess,
       * 派驻组评估说明 pzzIllustrate, 监督检查室评估说明 jcsIllustrate,
       * 操作时间 uploadTime,备注 remake)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationPlanList (id int primary key, ' +
        'ProblemClassification varchar(64), Problems varchar(64), ' +
        'FeedbackIssues varchar(64), target varchar(64), ' +
        'leadership varchar(64), personLiable varchar(64), ' +
        'timeLimitFoRCompletion varchar(64), pzzAssess varchar(64), ' +
        'concreteProblems varchar(64), jcsAssess varchar(64), ' +
        'pzzIllustrate varchar(64), jcsIllustrate varchar(64), ' +
        'uploadTime varchar(64), remake varchar(64))')
      /**
       * 建表 -- 整改方案审批记录 RectificationPlanSpjl(操作人 editor,
       * 提交时间 uploadTime,
       * 提交状态 status)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationPlanSpjl (id int primary key, editor varchar(64), ' +
        'status varchar(64),' +
        'uploadTime varchar(64))')
      /**
       * 建表 -- 整改报告详情 RectificationReport(
       * 标题 title,
       * 编辑人 editor,联系电话 telephone,
       * 审批状态 spstatus,处理状态（暂存【处理中】，保存【已处理】） clstatus,
       * 整改报告 RectificationReport,党风室评估情况 dfsAssess,
       * 操作时间 uploadTime,)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationReport (id int primary key, ' +
        'title varchar(64), ' +
        'editor varchar(64), ' +
        'telephone varchar(64), spstatus varchar(64), clstatus varchar(64), ' +
        'RectificationReport varchar(64), dfsAssess varchar(64), ' +
        'uploadTime varchar(64))')
      /**
       * 建表 -- 整改报告列表 RectificationReportList(
       * 问题分类 ProblemClassification,问题方面 Problems,
       * 反馈问题 FeedbackIssues,整改目标及具体措施 target,
       * 整改完成自评情况 assessment,
       * 责任领导 leadership,责任人 personLiable,
       * 完成时限 timeLimitFoRCompletion, 责任追究情况 zrzjAssess,
       * 清缴资金情况 qjzjAssess, 建立修订制度名称 systemName,
       * 党风室汇总认定 recognition, 党风室评估说明 dfsIllustrate,
       * 操作时间 uploadTime,备注 remake)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationReportList (id int primary key, ' +
        'ProblemClassification varchar(64), Problems varchar(64), ' +
        'FeedbackIssues varchar(64), target varchar(64), ' +
        'assessment varchar(64),' +
        'leadership varchar(64), personLiable varchar(64), ' +
        'timeLimitFoRCompletion varchar(64), zrzjAssess varchar(64), ' +
        'qjzjAssess varchar(64), systemName varchar(64), ' +
        'recognition varchar(64), dfsIllustrate varchar(64), ' +
        'uploadTime varchar(64), remake varchar(64))')
      /**
       * 建表 -- 整改报告审批记录 RectificationReportSpjl(操作人 editor,
       * 提交时间 uploadTime,
       * 提交状态 status)
       */
      db.run('CREATE TABLE IF NOT EXISTS RectificationReportSpjl (id int primary key, editor varchar(64), ' +
        'status varchar(64),' +
        'uploadTime varchar(64))')
      // 建表 -- 整改公开 PublicRectification(
      // 标题 title,编辑人 editedBy,
      // 审批状态 spstatus,处理状态（暂存【处理中】，保存【已处理】） clstatus,
      // 党内通报稿 notification,
      // 社会公开稿 publicManuscript,
      // 不宜向社会公开事项的说明 illustrate,
      // 操作时间 uploadTime)
      db.run('CREATE TABLE IF NOT EXISTS PublicRectification (id int primary key, ' +
        'title varchar(64), editedBy varchar(64), ' +
        'spstatus varchar(64), clstatus varchar(64), ' +
        'notification varchar(64), ' +
        'publicManuscript varchar(64), ' +
        'illustrate varchar(64), ' +
        'uploadTime varchar(64))')
      /**
       * 建表 -- 整改公开审批记录 PublicRectificationSpjl(
       * 发送时间 sendTime,
       * 审核单位 unit,
       * 操作人 editor,
       * 审批意见 opinion,
       * 审批说明 illustrate,
       * 说明材料 material,
       * 审批时间 approvalTime,
       * 提交时间 uploadTime)
       */
      db.run('CREATE TABLE IF NOT EXISTS PublicRectificationSpjl (id int primary key, ' +
        'sendTime varchar(64), ' +
        'unit varchar(64),' +
        'editor varchar(64),' +
        'opinion varchar(64),' +
        'illustrate varchar(64),' +
        'material varchar(64),' +
        'approvalTime varchar(64),' +
        'uploadTime varchar(64))')
      /**
       * 建表 -- 数据同步 DataSynchronization(
       * 文件 file,
       * 文件上传时间 uploaded,
       * 文件获取时间 acquisitionTime,
       * 导入时间 importTime,
       * 提交时间 uploadTime)
       */
      db.run('CREATE TABLE IF NOT EXISTS DataSynchronization (id int primary key, ' +
        'file varchar(64), ' +
        'uploaded varchar(64),' +
        'acquisitionTime varchar(64),' +
        'importTime varchar(64),' +
        'uploadTime varchar(64))')
      // 建表 -- 导入导出记录 ImportAndExportRecords(
      // 类型 type,操作时间 date,
      // 导入导出内容 content,文件 file)
      db.run('CREATE TABLE IF NOT EXISTS ImportAndExportRecords (' +
        'id int primary key, type varchar(64), ' +
        'date varchar(64), ' +
        'content varchar(64), file varchar(64))')
      // 建表 -- 应用调阅资料 AccessToMaterials(
      // 文件类型 fileType,
      // 文件名 fileName,
      // 路径 filePath,
      // 所属文件 documentsType,
      // 操作时间 uploadTime,)
      db.run('CREATE TABLE IF NOT EXISTS AccessToMaterials (' +
        'id int primary key, fileType varchar(64), ' +
        'fileName varchar(64), ' +
        'filePath varchar(64), ' +
        'documentsType varchar(64), ' +
        'uploadTime varchar(64))')
      resolve()
    })
  })
}
// 获取当前时间
function getcurDate() {
  var currentDate = new Date();
    var year = currentDate.getFullYear().toString();
    var month = (currentDate.getMonth() + 1).toString();
    if (month.length === 1){
        month = "0" + month
    }
    var date = currentDate.getDate().toString();
    if (date.length === 1){
        date = "0" + date
    }
    var hour = currentDate.getHours().toString();
    if (hour.length === 1){
        hour = "0" + hour
    }
    var minute = currentDate.getMinutes().toString();
    if (minute.length === 1){
        minute = "0" + minute
    }
    var second = currentDate.getSeconds().toString();
    if (second.length === 1){
        second = "0" + second
    }
    return year+"-"+month+"-"+date+" " + hour+":"+minute+":"+second
}
// 初始化密码
export const addPassWord = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare = db.prepare('insert into PassWord (id, password, uploadTime) values (?, ?, ?)')
    prepare.run(generateUUID(), product.password, getcurDate())
    prepare.finalize(err => {
      if (!err) resolve('操作成功')
    })
  })
}
// 查询密码
export const getPassWord = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from PassWord', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 修改密码
export const editPassWord = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare = db.prepare('UPDATE PassWord SET password = ?, uploadTime = ? WHERE id = ?')
    prepare.run(product.password, getcurDate(), product.id)
    prepare.finalize(err => {
      if (!err) resolve('操作成功')
    })
  })
}
// 查询反馈意见
export const getFeedback = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from Feedback', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询反馈公开
export const getPublicityOfFeedback = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from PublicityOfFeedback', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 新增/修改反馈公开
export const addPublicityOfFeedback = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE PublicityOfFeedback SET title = ?, editedBy = ?, content = ?, enclosure = ?, uploadTime = ? WHERE id = ?')
      prepare.run(product.title, product.editedBy, product.content, product.enclosure, getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into PublicityOfFeedback (id, title, editedBy, content, enclosure, uploadTime) values (?, ?, ?, ?, ?, ?)')
      prepare.run(generateUUID(), product.title, product.editedBy, product.content, product.enclosure, getcurDate())
    }
    prepare.finalize(err => {
      if (!err) {
        resolve('操作成功')
      } else {
        resolve('操作失败')
      }
    })
  })
}
// 查询整改会议 => 研究整改工作
export const getRectificationMeeting = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationMeeting', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 新增/修改整改会议 => 研究整改工作
export const addRectificationMeeting = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE RectificationMeeting SET theme = ?, meetingType = ?, dateOfMeeting = ?, startMeetingTime = ?, ednMeetingTime = ?, host = ?, participants = ?, meetingSummary = ?, uploadTime = ? WHERE id = ?')
      prepare.run(product.theme, product.meetingType, product.dateOfMeeting, product.meetingTime[0], product.meetingTime[1], product.host, product.participants, product.meetingSummary, getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into RectificationMeeting (id, theme, meetingType, dateOfMeeting, startMeetingTime, ednMeetingTime, host, participants, meetingSummary, uploadTime) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
      prepare.run(generateUUID(), product.theme, product.meetingType, product.dateOfMeeting, product.meetingTime[0], product.meetingTime[1], product.host, product.participants, product.meetingSummary, getcurDate())
    }
    prepare.finalize(err => {
      if (!err) resolve(true)
    })
  })
}
// 删除整改会议 => 研究整改工作
export const delRectificationMeeting = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare = db.prepare('DELETE FROM RectificationMeeting WHERE id = ?')
    prepare.run(product.id)
    prepare.finalize(err => {
      if (!err) resolve('操作成功')
    })
  })
}
// 查询整改方案详情
export const getRectificationPlan = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationPlan', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询整改方案列表
export const getRectificationPlanList = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationPlanList', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询整改方案审批记录
export const getRectificationPlanSpjl = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationPlanSpjl', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 新增/修改整改方案详情
export const addRectificationPlan = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE RectificationPlan SET editor = ?,' +
      'telephone = ?, spstatus = ?, clstatus = ?,' +
      'rectificationPlan = ?, pzzAssess = ?,' +
      'jcsAssess = ?, uploadTime = ? WHERE id = ?')
      prepare.run(
        product.editor,
        product.telephone, product.spstatus,
        product.clstatus, product.rectificationPlan,
        product.pzzAssess, product.jcsAssess,
        getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into RectificationPlan (id,' +
        'editor, telephone, spstatus, clstatus,' +
        'rectificationPlan, pzzAssess,' +
        'jcsAssess, uploadTime) values ' +
        '(?, ?, ?, ?, ?, ?, ?, ?, ?)')
      prepare.run(
        generateUUID(),product.editor,
        product.telephone, product.spstatus,
        product.clstatus, product.rectificationPlan,
        product.pzzAssess, product.jcsAssess,
        getcurDate())
    }
    prepare.finalize(err => {
      if (!err) resolve(true)
    })
  })
}
// 新增/修改整改方案列表
export const addRectificationPlanList = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE RectificationPlanList SET ' +
      'ProblemClassification = ?,' +
      'Problems = ?, FeedbackIssues = ?, target = ?,' +
      'leadership = ?, personLiable = ?,' +
      'pzzAssess = ?, concreteProblems = ?,' +
      'jcsAssess = ?, pzzIllustrate = ?,' +
      'jcsIllustrate = ?,' +
      'timeLimitFoRCompletion = ?, ' +
      'uploadTime = ? WHERE id = ?')
      prepare.run(
        product.ProblemClassification,
        product.Problems, product.FeedbackIssues, product.target,
        product.leadership, product.personLiable,
        product.pzzAssess, product.concreteProblems,
        product.jcsAssess, product.pzzIllustrate,
        product.jcsIllustrate,
        product.timeLimitFoRCompletion,
        getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into RectificationPlanList (id,' +
        'ProblemClassification, Problems, FeedbackIssues, target,' +
        'leadership, personLiable,' +
        'pzzAssess, concreteProblems,' +
        'jcsAssess, pzzIllustrate,' +
        'jcsIllustrate,' +
        'timeLimitFoRCompletion, uploadTime) values ' +
        '(?, ?, ?, ?, ?, ?, ?, ?, ?)')
      prepare.run(
        generateUUID(),product.ProblemClassification,
        product.Problems, product.FeedbackIssues, product.target,
        product.leadership, product.personLiable,
        product.pzzAssess, product.concreteProblems,
        product.jcsAssess, product.pzzIllustrate,
        product.jcsIllustrate,
        product.timeLimitFoRCompletion,
        getcurDate())
    }
    prepare.finalize(err => {
      if (!err) resolve(true)
    })
  })
}
// 查询整改报告
export const getRectificationReport = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationReport', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询整改报告列表
export const getRectificationReportList = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationReportList', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询整改报告审批历史
export const getRectificationReportSpjl = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from RectificationReportSpjl', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 新增/修改整改报告详情
export const addRectificationReport = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE RectificationReport SET editor = ?,' +
      'telephone = ?, spstatus = ?, clstatus = ?,' +
      'RectificationReport = ?, dfsAssess = ?,' +
      'uploadTime = ? WHERE id = ?')
      prepare.run(
        product.editor,
        product.telephone, product.spstatus,
        product.clstatus, product.RectificationReport,
        product.dfsAssess,
        getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into RectificationReport (id,' +
        'editor, telephone, spstatus, clstatus,' +
        'RectificationReport, dfsAssess,' +
        'uploadTime) values ' +
        '(?, ?, ?, ?, ?, ?, ?, ?)')
      prepare.run(
        generateUUID(), product.editor,
        product.telephone, product.spstatus,
        product.clstatus, product.rectificationPlan,
        product.dfsAssess,
        getcurDate())
    }
    prepare.finalize(err => {
      if (!err) resolve(true)
    })
  })
}
// 新增/修改整改报告列表
export const addRectificationReportList = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE RectificationReportList SET ' +
      'ProblemClassification = ?,' +
      'Problems = ?, FeedbackIssues = ?, target = ?,' +
      'assessment = ?,' +
      'leadership = ?, personLiable = ?,' +
      'timeLimitFoRCompletion = ?, ' +
      'zrzjAssess = ?, qjzjAssess = ?,' +
      'systemName = ?, recognition = ?,' +
      'dfsIllustrate = ?,' +
      'uploadTime = ? WHERE id = ?')
      prepare.run(
        product.ProblemClassification,
        product.Problems, product.FeedbackIssues, product.target,
        product.assessment,
        product.leadership, product.personLiable,
        product.timeLimitFoRCompletion,
        product.zrzjAssess, product.qjzjAssess,
        product.systemName, product.recognition,
        product.dfsIllustrate,
        getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into RectificationPlanList (id,' +
        'ProblemClassification, Problems, FeedbackIssues, target,' +
        'assessment' +
        'leadership, personLiable,' +
        'zrzjAssess, qjzjAssess,' +
        'systemName, recognition,' +
        'dfsIllustrate,' +
        'timeLimitFoRCompletion, uploadTime) values ' +
        '(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)')
      prepare.run(
        generateUUID(),product.ProblemClassification,
        product.Problems, product.FeedbackIssues, product.target,
        product.assessment,
        product.leadership, product.personLiable,
        product.zrzjAssess, product.qjzjAssess,
        product.systemName, product.recognition,
        product.dfsIllustrate,
        product.timeLimitFoRCompletion,
        getcurDate())
    }
    prepare.finalize(err => {
      if (!err) resolve(true)
    })
  })
}
// 查询整改公开
export const getPublicRectification = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from PublicRectification', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询整改公开审批记录
export const getPublicRectificationSpjl = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from PublicRectificationSpjl', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 新增/修改整改公开
export const addPublicRectification = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    if (product.id !== undefined) {
      prepare = db.prepare('UPDATE PublicRectification SET title = ?, editedBy = ?, content = ?, enclosure = ?, uploadTime = ? WHERE id = ?')
      prepare.run(product.title, product.editedBy, product.content, product.enclosure, getcurDate(), product.id)
    } else {
      prepare = db.prepare('insert into PublicRectification (id, title, editedBy, content, enclosure, uploadTime) values (?, ?, ?, ?, ?, ?)')
      prepare.run(generateUUID(), product.title, product.editedBy, product.content, product.enclosure, getcurDate())
    }
    prepare.finalize(err => {
      if (!err) {
        resolve('操作成功')
      } else {
        resolve('操作失败')
      }
    })
  })
}
// 查询数据同步
export const getDataSynchronization = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from DataSynchronization', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 查询导入导出
export const getImportAndExportRecords = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from ImportAndExportRecords', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 上传文件
export const uploadFile = (product) => {
  return new Promise((resolve, reject) => {
    let uuid = generateUUID()
    let fileExt = product.name.slice(product.name.lastIndexOf('.') + 1)
    //获取保存路径
    //设置保存路径
    const targetFolder = path.join(process.resourcesPath)
    const oldFilePath = path.resolve(product.path)
    const OriginFile = path.resolve(oldFilePath)
    const CopyFile = path.resolve(targetFolder, `${uuid}.${fileExt}`)
    fs.copyFileSync(OriginFile,CopyFile)
    compressing.zip.uncompress(CopyFile, path.resolve(targetFolder)).then((res) => {
      resolve({
        id: uuid,
        name: product.name,
        path: path.resolve(targetFolder)
      })
    }).catch((res) => {
      resolve('解压失败')
    })
    // fs.readFile(product.path, 'utf8', function (err, data) {
    //   console.log(err)
    //   console.log(data)
    // })
  })
}

// 查询调阅资料
export const getAccessToMaterials = () => {
  return new Promise((resolve, reject) => {
    let db = conn()
    db.all('select * from AccessToMaterials', (err, rows) => {
      if (err) reject(err)
      resolve(rows || [])
    })
  })
}
// 调阅资料上传
export const uploadAccessToMaterials = (product) => {
  return new Promise((resolve, reject) => {
    let uuid = generateUUID()
    let fileExt = product.name.slice(product.name.lastIndexOf('.') + 1)
    //设置保存路径
    const targetFolder = path.join(process.resourcesPath)
    const oldFilePath = path.resolve(product.path)
    const OriginFile = path.resolve(oldFilePath)
    const CopyFile = path.resolve(targetFolder, `${uuid}.${fileExt}`)
    fs.copyFileSync(OriginFile,CopyFile)
    resolve({
      id: uuid,
      fileType: fileExt,
      fileName: product.name,
      filePath: path.resolve(targetFolder, `${uuid}.${fileExt}`),
      documentsType: product.documentsType,
      uploadTime: getcurDate()
    })
  })
}
// 新增调阅资料
export const addAccessToMaterials = (product) => {
  return new Promise((resolve, reject) => {
    let db = conn()
    let prepare
    // 库新增字段
    // db.get(
    //   `select count(*) c from sqlite_master where name='AccessToMaterials' and sql like '%"zzz"%'`, (err, rows) => {
    //     if(rows.c == 0) {
    //       db.run('ALTER TABLE AccessToMaterials ADD COLUMN zzz varchar(64) DEFAULT NULL')
    //     }
    //   }
    // )
    prepare = db.prepare('insert into AccessToMaterials (id,' +
      'fileType,' +
      'fileName,' +
      'filePath,' +
      'documentsType,' +
      'uploadTime) values ' +
      '(?, ?, ?, ?, ?, ?)')
    prepare.run(
      product.id,
      product.fileType,
      product.fileName,
      product.filePath,
      product.documentsType,
      product.uploadTime)
    prepare.finalize(err => {
      if (!err) {
        resolve('操作成功')
      } else {
        resolve('操作失败')
      }
    })
  })
}
